after taking up the iterative approach and sorting out all of the little bugs I was finally able to get the right value