added tests and edge case guards to logic.R