refactored the code and added more logging