Fix #302 (with tests)