Additional unit tests and some code deduplication