Order attachments by path
Code clean-up and refactoring