Implemented the MWBM algorithm