REFACTOR: extract functions out of document.ready code