Media and PDF attachment have common base
General code improvements and tidied up