Various bug fixes: LUN-881, LUN-880, LUN-878

See separate commits if you want to look at individual fixes.