refactor the mess into subs
require perl 5.10 etc.