More refactoring to add unit tests & doc