fixed #706

raising the new PR as previous PR has multiple commits and misleading, also taken care of suggested changes. Please review.