Simplified dynamic attachments.
Integrated changes suggested by @fubhy in 79f17c1669c120840acef92626d3dc382fc71493.
Refactoring and simplifications.