first pass refactor. basic test works, but still needs docs, more thought