Fixed issue #1111 with better performance

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