Complete

Code finished. Some polishing and some comments are needed.
Final version

This is the final version of the code