Added comprehensive error checking to prevent user-induced crashes