Refactored the code and added logger