big refactor of js code using object literal