Updated error handling and prevent api from being too greedy.