Fix various invalid indices due to calculating "size() - 1" on empty containers.