Fixed some error handling issues