Refactored Javascript to make better use of structured objects.