flake8 fixes, typos and some code optimizations