rewrite code and works for a sequence of ints