I'm almost sure it logically does what you wrote the code for, but what did you mean for it to do?
What did your test results show? What tests did you do? Are there other tests you can try in...