fixed DB helper function and made conditions more robust