Fix bugs in buildScop.