Refactoring to use more jQuery and make it more readable