Code optimizations + general cleanup