added push/pop/removestate primitives, and supporting code.