Fixed issue #1111 with better performance

Fixed issue #1111 with better performance
Thanks to darkager for pointing this out.