Changed to original algorithm that used divide and conquer logic