This works only for finite sequences not for arbitrary.