add cleanup_mb_if_dirty routine and refactor the code