code converted from haxe