Refactored function so it does not use $.fn namespace