Merge jamie patches

Fix for merging Jamie's recent patches.