only works but not with edge cases