refactored code. added log statements