Refractored the code