Refactored the code to make use of promises