I made some changes thanks to your ideas and got it working on dynamic tilemaps (adding and removing tiles):