More refined version of same code.