refactored code for jQuery