Add tests and refactor upload function