Fixes #2 and other bug fixes.