modified the handling of loops a bit, but it isnt working