Optimizations, cleanup, and various improvements