Performance optimizations

Thanks @bukka