copied from lohit2 with some modifications for copy reference