Refactored some JavaScript