Issue #1010: $dp1 not defined

Turned out that the script aborts early on a few places if it can't find the data it needs. Had a look and try to make the returns consistent throughout the file.

Note: I didn't have a chance to test all scenarios because I don't have a lot of data on my DB.