small performance optimization: O(n)+1