Refactor to use promises to manage asynchronous callbacks.