this is the O(n) implementation)