Fix various errors and inconsistencies

Make the application more robust by implementing better error handling.