Fixes for bugs found by Tom and James