Fix #13 and a few other bugs.