this isn't at all the right solution, but this code is already a nasty mess, so I'm inclined to leave it for now.