Refactor jquery masonry code