cleaned and refactored code for direct upload