refractored a function