Added comments and cleaned up. Removed some unused functions.