Added code to handle scope better