It seems like a lot of these functions could be replaced with a lookup table.