reorg code for better lib consumption
fix unit tests