I don't like this long if ... elif ... block, but I can't think of a more elegant way of doing it.