Factored out code relating to media files (which is a lot of code, so good for me)