rewrote javascript in a more modular way