refactor legalizer