fixed to handle special characters properly and provide more accurate preview