A few bug fixes and code optimization.