clean up lua code and add inline comments