d is going out of bounds. need to fix.