Optimized code and fixed bugs