Refactored a bit to make the code more readable and to conform to PEP8 standards.