Adds tests; small refactors.