Fix bugs in attachment handling.