added support for scope and re arranged code