port Fort Lonestar to Lua

structure the code better