More optimizations and some pep8 changes