First implementation, use stack