Reformatted some code to better match conventions, added comments