refactor out is_legal method