bugfix and improved logging