fix #28 and other bugs