Refactored lots of the python code into a package.