Updated and fixed by Boris Batyunya for consitancy with new/fixed SPD code.