Not an issue in ubase but someone might want to re-use this function elsewhere.
Needs a bit of cleanup (avoiding magic numbers etc.).