Fixed bug where loop ends and count=0