Code refactoring to use promises instead of nested callbacks.