javascript code structured better