Made changes suggested by Brian, added test.