refactoring C part of #2611