Great idea outsourcing this. This became pretty hairy.