Copied error-handling logic over from old app