Bits of attachments refactoring

Note: untested at this time. I'd PR it for review only.
What it does: get out of the way a few duplicated code or queries.