Refactoring $stack

Time: 235 ms, Memory: 21.00Mb
OK (254 tests, 256 assertions)

Change-Id: Iba4ff0d55c2b37c55965cea11f7d6e71ae21bf2a