Series of bug fixes. closes #150.

A series of bug fixes. See specific commits for details.