Minor improvement on testability and added some unit tests.