Clean up this horrifying code.