Documented and cleaned up JavaScript code