Refactored to use SparseArray.
Refactored to give better name.
Added comments.