fix some bugs, fixes #16, fixes #17