A few bug fixes and more error handling