Simplify the patch functions; change some error outputs to assertions.