Refactoring and optimizations (jQuery)