Added retry option for retrieval, cleaned up code