lots more unit tests and bug fixing