added tests and code fixes