Fixed bug highlighted by tests.