Clean up and idiomize code
Generalize test code