Resolved issues #856 and #866

This is a cumulative PR, containing fixed for issues 856 and 866. The build fails when either of them isn't resolved, so we're merging them altogether here.