Refactor and extract JQuery code