Changed (* n -1) to (- n) and same for d