Fix #39, though should be tested more thoroughly.