Rewritten version of MMD with cleaner code