Optimizations and some cleanup.