Changed stack to a deque, so we don't have to reverse it.