Fix for #1686

Adding an OR in the query was short-circuiting the logic.  We need to
group things with parentheses.
Merge pull request #1688 from pculture/gh-1686

Fix for #1686
Fixes for deleted external accounts

Fixed a couple functions so they don't crash if an account is deleted.