Please add more comments in your function, so that future maintainers of your code understand what is going on and why you chose to do things this way.