added @cpstubing's code as per issue #1