Here is the solution to your problem:
This was a tricky one to spot, and I corrected two things in your patch. The first was not really necessary, but looks better. You were feeding values with decimal point to both [until] and [counter], but they do convert them to integer before processing. But that means that the numbers you were seeing as total samples and start position were not really spot on, so that's why I added those two [int].
But the main problem was that you were starting to write $0-array at the position start_pos instead of the start of the array (that is, position 0). By subtracting the start_pos, you are able to write the $0-array from it's initial position, thus avoiding any problems.
I hope this helps. Take care,