Fix bugs in attachment handling.
Add more logging; flip around bad-security detection order.