Corrected bugs and made the code pep8 compliant.