Add unit tests (plus minor changes)