Continuation of 24e0565. In order to apply the new LWS/GWS scheme, some
shared code is moved to a new function.