fixed implementation and added tests