Reorganise and comment code