Fixes #23 and other bug fixes.