Major refactoring, using promises