performance improvements for issue #585 and #588