Here is a working version of this program. First commit.