Improved promise-code.

I removed some layers of nesting for the promise-code, hopefully increasing readability!

I have to confess I didn't do a lot of bugchecking. Would be nice to have a bunch of example scripts to easily test if everything works :).