Added comments to further explain functions and tests. Also cleaned up code.