refactor code so not bound to dom element