This isn't pretty but it works.