Fix #856
Fix #1107
Very light refactoring