Producing correct answers but hangs occasionally