adding unit tests and fixing found bugs.