Refactored JavaScript into something a little tidier and less redundant;