changed shift function to be non recursive