Added comments and neatened up code