diff --git a/src/common/connection_handlers.py b/src/common/connection_handlers.py
index 9cdc6fd9de12035749d75d18d0036cb1bd2c2158..a0fee6fa64869adf8107a853e26dd0106900ab71 100644
--- a/src/common/connection_handlers.py
+++ b/src/common/connection_handlers.py
@@ -24,7 +24,6 @@ import sha
 import socket
 
 from calendar import timegm
-from encodings.punycode import punycode_encode
 
 import socks5
 import common.xmpp
@@ -750,8 +749,7 @@ class ConnectionVcard:
 
 	def save_vcard_to_hd(self, full_jid, card):
 		jid, nick = gajim.get_room_and_nick_from_fjid(full_jid)
-		nick = nick.replace('/', '_')
-		puny_jid = punycode_encode(jid)
+		puny_jid = helpers.sanitize_filename(jid)
 		path = os.path.join(gajim.VCARD_PATH, puny_jid)
 		if jid in self.room_jids:
 			# remove room_jid file if needed
@@ -760,7 +758,7 @@ class ConnectionVcard:
 			# create folder if needed
 			if not os.path.isdir(path):
 				os.mkdir(path, 0700)
-			puny_nick = punycode_encode(nick)
+			puny_nick = helpers.sanitize_filename(nick)
 			path_to_file = os.path.join(gajim.VCARD_PATH, puny_jid, puny_nick)
 		else:
 			path_to_file = path
@@ -773,10 +771,9 @@ class ConnectionVcard:
 		return {} if vcard was too old
 		return None if we don't have cached vcard'''
 		jid, nick = gajim.get_room_and_nick_from_fjid(fjid)
-		nick = nick.replace('/', '_')
-		puny_jid = punycode_encode(jid)
+		puny_jid = helpers.sanitize_filename(jid)
 		if is_fake_jid:
-			puny_nick = punycode_encode(nick)
+			puny_nick = helpers.sanitize_filename(nick)
 			path_to_file = os.path.join(gajim.VCARD_PATH, puny_jid, puny_nick)
 		else:
 			path_to_file = os.path.join(gajim.VCARD_PATH, puny_jid)
@@ -944,11 +941,11 @@ class ConnectionVcard:
 			# Save it to file
 			self.save_vcard_to_hd(who, card)
 			# Save the decoded avatar to a separate file too, and generate files for dbus notifications
-			puny_jid = punycode_encode(frm)
+			puny_jid = helpers.sanitize_filename(frm)
 			puny_nick = None
 			begin_path = os.path.join(gajim.AVATAR_PATH, puny_jid)
 			if frm in self.room_jids:
-				puny_nick = punycode_encode(resource.replace('/', '_'))
+				puny_nick = helpers.sanitize_filename(resource)
 				# create folder if needed
 				if not os.path.isdir(begin_path):
 					os.mkdir(begin_path, 0700)
diff --git a/src/common/helpers.py b/src/common/helpers.py
index d284b52c271e72a64cc19a36cf414c393a01f5ec..4a8a766a105a64d2e4bd535452f109c7129f1318 100644
--- a/src/common/helpers.py
+++ b/src/common/helpers.py
@@ -24,6 +24,7 @@ import sys
 import stat
 import sha
 from pysqlite2 import dbapi2 as sqlite
+from encodings.punycode import punycode_encode
 
 import gajim
 import logger
@@ -702,12 +703,14 @@ def get_os_info():
 def sanitize_filename(filename):
 	'''makes sure the filename we try to write does not contain
 	unacceptable characters'''
+	filename = punycode_encode(filename)
 	filename = filename.replace('/', '_')
 	if os.name == 'nt':
 		filename = filename.replace('?', '').replace(':', '').replace('!', '')\
 			.replace('"', "'")
 	   # 48 is the limit
 		if len(filename) > 48:
+			extension = filename.split('.')[-1]
 			filename = filename[0:48]
 	
 	return filename
diff --git a/src/gajim.py b/src/gajim.py
index 6c0d9c5e588dfe76b6bf28ca49b62e3c0cdc9ab2..61027552083c9b75f784b121679bab0cb173e52e 100755
--- a/src/gajim.py
+++ b/src/gajim.py
@@ -95,7 +95,6 @@ import signal
 import getopt
 import time
 import threading
-from encodings.punycode import punycode_encode
 
 import gtkgui_helpers
 import notify
@@ -994,7 +993,7 @@ class Interface:
 
 	def save_avatar_files(self, jid, photo_decoded, puny_nick = None):
 		'''Save the decoded avatar to a separate file, and generate files for dbus notifications'''
-		puny_jid = punycode_encode(jid)
+		puny_jid = helpers.sanitize_filename(jid)
 		path_to_file = os.path.join(gajim.AVATAR_PATH, puny_jid)
 		if puny_nick:
 			path_to_file = os.path.join(path_to_file, puny_nick)
diff --git a/src/gtkgui_helpers.py b/src/gtkgui_helpers.py
index 94d178ca29dc1421648d25faeafed5357276edde..11e60b1ca691321ddc639374f2287138ff1380a8 100644
--- a/src/gtkgui_helpers.py
+++ b/src/gtkgui_helpers.py
@@ -23,7 +23,6 @@ import gobject
 import pango
 import os
 import sys
-from encodings.punycode import punycode_encode
 
 import vcard
 
@@ -435,15 +434,14 @@ def get_avatar_pixbuf_from_cache(fjid, is_fake_jid = False):
 	so we have new sha) or if we don't have the vcard'''
 
 	jid, nick = gajim.get_room_and_nick_from_fjid(fjid)
-	nick = nick.replace('/', '_')
 	if gajim.config.get('hide_avatar_of_transport') and\
 		gajim.jid_is_transport(jid):
 		# don't show avatar for the transport itself
 		return None
 
-	puny_jid = punycode_encode(jid)
+	puny_jid = helpers.sanitize_filename(jid)
 	if is_fake_jid:
-		puny_nick = punycode_encode(nick)
+		puny_nick = helpers.sanitize_filename(nick)
 		path = os.path.join(gajim.VCARD_PATH, puny_jid, puny_nick)
 	else:
 		path = os.path.join(gajim.VCARD_PATH, puny_jid)
@@ -488,7 +486,7 @@ def get_path_to_generic_or_avatar(generic, jid = None, suffix = None):
 	Returns full path to the avatar image if it exists,
 	otherwise returns full path to the image.'''
 	if jid:
-		puny_jid = punycode_encode(jid)
+		puny_jid = helpers.sanitize_filename(jid)
 		path_to_file = os.path.join(gajim.AVATAR_PATH, puny_jid) + suffix
 		if os.path.exists(path_to_file):
 			return path_to_file
diff --git a/src/tooltips.py b/src/tooltips.py
index 3d0e380d50bdf8b92582ff3bba7db568d04ef139..dd2ef2ddf8ebd6ca14afb86695eb0b578de4454f 100644
--- a/src/tooltips.py
+++ b/src/tooltips.py
@@ -19,7 +19,6 @@ import gobject
 import os
 import time
 import locale
-from encodings.punycode import punycode_encode
 
 import gtkgui_helpers
 import message_control
@@ -395,8 +394,8 @@ class GCTooltip(BaseTooltip):
 		properties.append((_('Status: '), show))
 		
 		# Add avatar
-		puny_name = punycode_encode(contact.name)
-		puny_room = punycode_encode(contact.room_jid)
+		puny_name = helpers.sanitize_filename(contact.name)
+		puny_room = helpers.sanitize_filename(contact.room_jid)
 		for type_ in ('jpeg', 'png'):
 			file = os.path.join(gajim.AVATAR_PATH, puny_room,
 				puny_name + '.' + type_)
@@ -475,7 +474,7 @@ class RosterTooltip(NotificationAreaTooltip):
 			if not iconset:
 				iconset = 'dcraven'
 			file_path = os.path.join(gajim.DATA_DIR, 'iconsets', iconset, '16x16')
-		puny_jid = punycode_encode(prim_contact.jid)
+		puny_jid = helpers.sanitize_filename(prim_contact.jid)
 		table_size = 3
 		
 		for type_ in ('jpeg', 'png'):