port from rev. 586
Merge commit '07a000eeeede08a6c49458abc38c21fa57ebdc2f'