Fixed bug in pd function