Re-wrote Perl version with module for extensibility.