reorganising and tidying up of upload handling code