Added what I think may work as a full implementation.