refactoring to better use the when promise framework