Completely changes the loop unwinding code and added documentation