Lots of clean up. Added remainder of necessary functions. Improved variable names and added comments.