Gosh that's some ugly code