This is rough code, but it works.