Updating my implementation after viewing Bill the Lizard's

It seems that my original implemnetation was not as accurate - must
have screwed up my definition of the function d