working rewrite of attachments script