avoid IndexError due to popping from empty list