neatened up some code and added comments