Added more error handling

Updated various files, added error and exception handling, prevented
different crashes that could happen in certain circumstances.