Fix infinity loop and more