Modified flatten to return an IndexedSequence

I noticed than flatten was returning a basic sequence, rather than an indexed sequence, which contradicted the type definition, so I made a little tweak to clear that up. The __iterate method of flattened sequences was returning the wrong count too, which was causing some subtle bugs.

I've added tests for the stuff I changed.