If you want to understand this more check the code of this project https://scratch.mit.edu/projects/469138957/editor/