Speed optimizations and formatting cleanup