Reduce complexity and fix PEP8, PEP257 style issues