made changes in implementing cost matrix