fixes issue #2599 and #2540