Simplify some convoluted code