replaced tmp vars with std stacks