Refactor duplicate code

Lots of duplicate code between SugarCRM and Wordpress. Adding Drupal
will make this worse, so clean it up now :)
Merge pull request #7 from fredemmott/cleanup

Refactor duplicate code