Changed debug loop to write to file instead of console.