I've just made a small refactoring to this code. please rebase your change. the code should now be easier to read.

see ee2689051bb40794eeb81baf80dc0717fd9edd2a