using stack, works with test inputs