Renamed a few things for clarity, added comments