Not sure why you're making it so complicated. The code I linked above (dsteem version) is exactly what I'm using and it spits out the same numbers you'll find on steemd.com. The code you shared seems fine, so I can't tell based on that where the problem might lie, besides that Math.min(withdraw_rate.amount, already_withdrawn) is missing.
RE: Help us fix a DLease Bug - 200 BUILDTEAM + 200 STEEM Bounty