Found a serious glitch thanks to print debugging. Fixed it. Still buggy but it's better than before.