Fixed a lot of error handling