Neatened up code a little bit and added more comments