Refactored most of the python code.