Refactor for clarity; working version.  Needs additional features like "push".