In my case, what has worked is to completely clean the main.lua file, and start from scratch, with the documentation in hand. this is like anything in life, to learn to play music, you learn by playing an instrument, and the same thing happens, you learn to program by programming.
So what I do is to have the main.lua file and many files represented in objects through the use of tables (classes), and then these objects communicate with each other. The example is not to build a wall of a single block, as in prefabricated houses, but as is done here in Latin America, walls with several blocks, bricks that make up our program, if one fails it will not be necessary to knock down the entire wall. This has taken me several years to learn after trial and error.
But I am sure that if you put your heart into it, you will learn something new and that is gratifying when we gain experience in something and make it applicable.