Second implementation, cache-friendly