Further attempts at simplifying this code.