Refactor and partly parallelise stack update code

Make this easier to read, and also use co-routines to make it run in
parallel where possible.

Change-Id: Ib37f4c04bd250cb15604ba9adb4cca8a97a2c4fb
Merge "Refactor and partly parallelise stack update code"