code refined for pan function