improved unwinding code
manually merged with master