Fix infinite promise recursion and pass unit tests