added download functionality and tidied up