First pass. Minimal error checking, but it works otherwise.