Implement auto-detection of resumable lambdas.
Rearrange and tidy up code.