Refactor and ajaxify attachments