Added retry option for failed attempts, plus a bit of cleaning