comits patch 23 from #121309, thanks to munga for the patch. Also does some code clean up to better adhere to drupal coding standards