moved inner function to outside