Changed slicing to reversed() in some places where it is sufficient to use an iterator.