Modularized the Existing Code, needs testing