Bug fixes. Now successfully runs in a time loop.