refactoring code to use a function to upload a file