Added logic to resolve issue #3 as per Stefan Csomors suggestion