Most of the following code was moved, and isn't new.