Moved some error-handling functions into the lib