Fix bugs in stack. Add tests.

Signed-off-by: Michael Burbidge <mburbidg@gmail.com>