C and Lua versions written by Szilard Pfeiffer and Viktor Tusa