Slightly more efficient insertion algorithm