Bug fixes and better logging