BM algo copied version, need more research