Fix code to use Promises and also handle invalid responses