Updated script to use pop instead of shift for efficiency reasons