Refactoring Code

A lot of changes to bring the code into line with pep8.