This code works, but takes FOREVER to run