Fix unittest. Overhauled internal stack implementation.

Fix protection bug. Fix and improve unused/underused variable detection.