Fixed some error handling