Some fixes and error handling