first version of the code

This is the initial version of the code, and needs to be optimized and
modularized.