fixed bugs and improved logging