Repetition isn't always a bad thing, and sometimes your algorithm just requires it; that is why you functionalize repetitious code so you can just call the the function instead of copying and pasting the code over and over.
You can't always help repetitious code. Your script is so small anyways, and it seems you have functionlized the repetitious code. Someone will always be able to tell you how they "feel" your code could be changed to be better, sometimes it is actually true, some times it is just opinion.
The big thing I would say, is take your "offset" variable which is global and define it at the top of your script. That way it is clear it is global, and someone coming in behind you doesn't have to search functions looking for the global term to find out it is in fact a global function and you didn't just forget to define it.
Some people even go further and do things like g_offset or gOffset to denote globals; again it is all personal preference, but globals are always a pain to identify, so at least list them at the beginning of the script to make it easier to tell they are global. In a small script like this, it isn't a big deal, but as scripts get larger, it is nice to be able to look at the top of the script and tell what variables are global.