Some cleanup and optimizations