tweaked code and added log messages