refactored code in few routines