Added logging and fixed infinite loop