refactored streaming mmd and linear time mmd