Adds more error handling and fixes