Fixed bug, and added test case for it.