functional consistency, fix global variable, refactor .done .fail to .then