reorganization of javascript code