Made code PEP8-correct, refactored, commented.