Reorganise and cleanup dead code