sync with Richard
Fixes #38 and fixes #51
Resolved conflicts.