Refactored much of the front-end code to remove repeated code.