refactored

refactored. Switched to prototypes. Renamed functions and variables. Added different comments. Functionality unchanged.