add log statements and fix some bugs