added tests and simplified code