modified code for better logging