Fixed bug that made the program crash occasionally