use closure and parameters instead of globals