Another rewrite of upload functions. A lot more robust. Need to improve matching regex.