Simplify a nested cond somewhat. Clean some code, and add some tests