properly fix #1166, test cases included