incoorporated Joey's updated code that fixes bug #2, another bug remain that causes 6 out of 1000 large l test cases to fail