refactors a bit jquery code