was crashing with a large input, so added to_s method after the pop() and it worked