restructuring, more unit tests, bug fix.