Added comments and neatened up the code.