reformatted some code, added comments