Refactored the code to increase modularity

The code is broken now into several modules which are organized
according to functionality.