bug fixes and added tests.