New version without global variables