Error handling and other fixes