Refactor the Perl code to be not a hack.