First full working code. attract loop no longer hangs.