Split this entire operation to a helper with a descriptive name. Hard to know what the heck it's doing from just reading it ;)