This method is getting very long. How do we simplify? I suspect we need to move the sql into helper methods.