Restructured the source code for readability and to comply with PEP8 rules.