Fix for issue #83: code uses db result before it exists

See comment thread attached to #83 for details and discussion.
Merge pull request #87 from usernamenumber/issue_83

Fix for issue #83: code uses db result before it exists