Updated implementation, added tests.