[CD] Original implementation.

You can see a lot TODOs and bugs in this version. Try out figure out
all the problems in the code, so that we can discuss.