faster implementation that is still too slow