Refactored, commented, and compatable with python 2.74 and python 3