debugged doubling code for edge cases of small arrays