debugging fixes and loop restoration