Fixes #26 and other bug fixes.