refactorize code affinitic refs #5735