maybe something for a function (by Volker Mueller...)
otherwise I would do it about the same way. Maybe you could remove some nodes for widthTotal and lenghtTotal to slim it a bit down.
Ingo