Revision: this isn't pretty but it doesn't work.