logic correction

The previous version was not giving the correct answers. I tested this one and the value appears correct.