Modularized the entire code
merged from mark to master