Factor out total code. Switch to pointers.