Bug fixes and error logging