Fixed a few errors, added more error handling