Fix bug in SparseTable::flatten

Fix bug with dereferencing map::end() iterator while flattening sparse
table
Add comments

Key classes and methods are commented