Fixes #2 and other bugs.