fix bug and add comments for each stage