promises plus ad-hoc refactoring and bug fixes