Fix code assuming that a list always has a tail