more code cleanup and logical separation #3847

#3847