Cleans up code, adds clarifying comments