fix #10 and various other bugs