fix #1145 and related bug