Reorganise js code