little refactor, add more unit tests, better docs