This might be the ugliest Python code I have ever seen. Seriously, was there no better way?