fix an issue where uploading file of same filename more than once doesn't work. Also tidy up code