Fix attachement processing

We debugged this with @marekkalnik and found this algorithm was wrong. The weird thing is that it worked on Chrome but not on Safari...
Add failing test