Refactored the promise code a bit