The onDamaged event handler would still fire after an enemy building had been captured. Fixed by checking the owner before starting repairs. Thanks to Microbit for showing me that people actually did that.