More tests; minor refactoring