Clean up

Re ordered to better group functions of similar use. Added more comments and removed some unneeded variables