Refactoring JavaScript code to be more readable/organized