Solved the mystery bug that kills my program after a while.