Add support for gif and jpeg files
Refactoring and tidying up