Refractored code and added logger