reorganise some code