added retry attempts, refactored