refactoring and fleshing out tests, fixing bugs