shop down some code complexity and add new unit tests