Error handling and misc fixes