fix M306 as per qharley (with improvements)