Complete functional version for big input