Fixed bug in code revealed by failing unit test