Fixed issue #1111 with better performance