Fixed #40
Added code from zachriggle's pull request #41