Issue-39

Pull request for issue #39. Needs some further testing before merge.