Merge in code from the rework-function code. Don't quite know how it works yet.