refactored to foldl-incremental