Pull request 155

This is PR #155 rebased.  It also fixes some of the issues identified in the code review for the original PR.