clean up remove a lot of duplicate code
add IfThen branch
refine names:fix
refine names :fix