I think I've messed around with this area in a separate pull request - I'm happy to clean up the merge conflicts.