PEP8 cleanup and minor optimizations