Ported and fixed for XE