add tests; fix bugs; initial clean up