C can runtime, this code is pretty ugly