You might consider approaching the problem in a different way. I like this approach and it’s easy to understand.
There are some other thread on here - like this one.