mostly refactored. making robust. no retry yet