refactored js into functions