Refactoring an attachment function