Fixed promise chaining
General refactoring