Rewritten with promises, but doesnt work