added tests and fixed a bug