Would a helper function not make this much simpler to read (and less code)?