Bug fixes, improved logging