Code refactoring

This refs affinitic #5785