Changed error handling quite a bit