Modified for inclusion in Drupal module.