scope function almost finished