added tests, fixed logic issues