Minor optimizations and fixes

Thanks @levifan