refractored some code