Fixed infinite loop bug, but causes a regression for issue 3