Fixed bugs and optimized code.