@matt: your function seems to make sense. i don't have the time at the moment to test this myself though!