Fix a few bugs, fixes #1867