Fixed some formatting and optimized code