Added improved mdc function