Minor optimization: use foreach rather than map