Rebase PR37

The PR is simply a rebase of Clair's PR #37 against master and then fixes to handle the changes that had happened in master.