fix a few bugs. add a loop option