Code improvements and optimizations