added debugging
Fixed critical bug
Added protection