reorganized javascript code