Mostly optimizations, cleanup, and comments