cleanup get_support_ticket
diff --git a/startup/schedule_handlers.py b/startup/schedule_handlers.py
index 668c11d..54b9892 100644
--- a/startup/schedule_handlers.py
+++ b/startup/schedule_handlers.py
@@ -26,7 +26,7 @@
 		* recurring invoice
 	"""
 	# pull emails
-	from support.doctype.support_ticket import get_support_mails
+	from support.doctype.support_ticket.get_support_mails import get_support_mails
 	run_fn(get_support_mails)
 	
 	# bulk email
diff --git a/support/doctype/support_ticket/__init__.py b/support/doctype/support_ticket/__init__.py
index 316eff8..e69de29 100644
--- a/support/doctype/support_ticket/__init__.py
+++ b/support/doctype/support_ticket/__init__.py
@@ -1,125 +0,0 @@
-# ERPNext - web based ERP (http://erpnext.com)
-# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
-# 
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-# 
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-# 
-# You should have received a copy of the GNU General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-from __future__ import unicode_literals
-import webnotes
-from webnotes.utils import cstr, cint
-
-from webnotes.utils.email_lib.receive import POP3Mailbox
-
-class SupportMailbox(POP3Mailbox):
-	def __init__(self):
-		self.email_settings = webnotes.doc("Email Settings", "Email Settings")
-		s = webnotes.doc('Support Email Settings')
-		s.use_ssl = self.email_settings.support_use_ssl
-		s.host = self.email_settings.support_host
-		s.username = self.email_settings.support_username
-		s.password = self.email_settings.support_password
-		
-		POP3Mailbox.__init__(self, s)
-	
-	def check_mails(self):
-		self.auto_close_tickets()
-		return webnotes.conn.sql("select user from tabSessions where \
-			time_to_sec(timediff(now(), lastupdate)) < 1800")
-	
-	def process_message(self, mail):
-		from home import update_feed
-			
-		thread_list = mail.get_thread_id()
-
-		for thread_id in thread_list:
-			exists = webnotes.conn.sql("""\
-				SELECT name
-				FROM `tabSupport Ticket`
-				WHERE name=%s AND raised_by REGEXP %s
-				""" , (thread_id, '(' + mail.from_email + ')'))
-			if exists and exists[0] and exists[0][0]:
-				st = webnotes.get_obj('Support Ticket', thread_id)
-				
-				from core.doctype.communication.communication import make
-				
-				make(content=mail.content, sender=mail.from_email, 
-					doctype="Support Ticket",
-					name=thread_id, lead = st.doc.lead, contact=st.doc.contact)
-				
-				st.doc.status = 'Open'
-				st.doc.save()
-				
-				update_feed(st, 'on_update')
-				# extract attachments
-				mail.save_attachments_in_doc(st.doc)
-				return
-				
-		from webnotes.model.doctype import get_property
-		opts = get_property('Support Ticket', 'options', 'naming_series')
-		# new ticket
-		from webnotes.model.doc import Document
-		d = Document('Support Ticket')
-		d.description = mail.content
-		
-		d.subject = mail.mail['Subject']
-		
-		d.raised_by = mail.from_email
-		d.content_type = mail.content_type
-		d.status = 'Open'
-		d.naming_series = opts and opts.split("\n")[0] or 'SUP'
-		try:
-			d.save(1)
-			mail.save_attachments_in_doc(d)
-		except:
-			d.description = 'Unable to extract message'
-			d.save(1)
-		else:
-			# send auto reply
-			if cint(self.email_settings.send_autoreply):
-				if "mailer-daemon" not in d.raised_by.lower():
-					self.send_auto_reply(d)
-		
-	def send_auto_reply(self, d):
-		from webnotes.utils import cstr
-
-		signature = self.email_settings.fields.get('support_signature') or ''
-
-		response = self.email_settings.fields.get('support_autoreply') or ("""
-A new Ticket has been raised for your query. If you have any additional information, please
-reply back to this mail.
-		
-We will get back to you as soon as possible
-----------------------
-Original Query:
-
-""" + d.description + "\n----------------------\n" + cstr(signature))
-
-		from webnotes.utils.email_lib import sendmail		
-		
-		sendmail(\
-			recipients = [cstr(d.raised_by)], \
-			sender = cstr(self.email_settings.fields.get('support_email')), \
-			subject = '['+cstr(d.name)+'] ' + cstr(d.subject), \
-			msg = cstr(response))
-		
-	def auto_close_tickets(self):
-		webnotes.conn.sql("""update `tabSupport Ticket` set status = 'Closed' 
-			where status = 'Waiting for Customer' 
-			and date_sub(curdate(),interval 15 Day) > modified""")
-
-
-def get_support_mails():
-	import webnotes
-	from webnotes.utils import cint
-	if cint(webnotes.conn.get_value('Email Settings', None, 'sync_support_mails')):
-		SupportMailbox().get_messages()
\ No newline at end of file
diff --git a/support/doctype/support_ticket/get_support_mails.py b/support/doctype/support_ticket/get_support_mails.py
new file mode 100644
index 0000000..204ca34
--- /dev/null
+++ b/support/doctype/support_ticket/get_support_mails.py
@@ -0,0 +1,94 @@
+# ERPNext - web based ERP (http://erpnext.com)
+# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
+# 
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+from __future__ import unicode_literals
+import webnotes
+from webnotes.utils import cstr, cint
+from webnotes.utils.email_lib import sendmail		
+from webnotes.utils.email_lib.receive import POP3Mailbox
+from core.doctype.communication.communication import make
+
+class SupportMailbox(POP3Mailbox):	
+	def setup(self):
+		self.email_settings = webnotes.doc("Email Settings", "Email Settings")
+		self.settings = webnotes._dict({
+			"use_ssl": self.email_settings.support_use_ssl,
+			"host": self.email_settings.support_host,
+			"username": self.email_settings.support_username,
+			"password", self.email_settings.support_password
+		})
+	
+	def check_mails(self):
+		self.auto_close_tickets()
+		return webnotes.conn.sql("select user from tabSessions where \
+			time_to_sec(timediff(now(), lastupdate)) < 1800")
+	
+	def process_message(self, mail):
+		thread_id = mail.get_thread_id()
+		ticket = None
+
+		if thread_id and webnotes.conn.exists("Support Ticket", thread_id):
+			ticket = webnotes.model_wrapper("Support Ticket", thread_id)
+			ticket.doc.status = 'Open'
+			ticket.doc.save()
+				
+		else:
+			ticket = webnotes.model_wrapper([{
+				"doctype":"Support Ticket",
+				"description": mail.content,
+				"subject": mail.mail["Subject"],
+				"raised_by": mail.from_email,
+				"content_type": mail.content_type,
+				"status": "Open"
+			}])
+			ticket.insert()
+
+			if cint(self.email_settings.send_autoreply):
+				if "mailer-daemon" not in mail.from_email.lower():
+					self.send_auto_reply(ticket.doc)
+
+		mail.save_attachments_in_doc(ticket.doc)
+				
+		make(content=mail.content, sender=mail.from_email, 
+			doctype="Support Ticket",
+			name=thread_id, lead = st.doc.lead, contact=st.doc.contact)
+
+	def send_auto_reply(self, d):
+		signature = self.email_settings.fields.get('support_signature') or ''
+		response = self.email_settings.fields.get('support_autoreply') or ("""
+A new Ticket has been raised for your query. If you have any additional information, please
+reply back to this mail.
+		
+We will get back to you as soon as possible
+----------------------
+Original Query:
+
+""" + d.description + "\n----------------------\n" + cstr(signature))
+
+		sendmail(\
+			recipients = [cstr(d.raised_by)], \
+			sender = cstr(self.email_settings.fields.get('support_email')), \
+			subject = '['+cstr(d.name)+'] ' + cstr(d.subject), \
+			msg = cstr(response))
+		
+	def auto_close_tickets(self):
+		webnotes.conn.sql("""update `tabSupport Ticket` set status = 'Closed' 
+			where status = 'Waiting for Customer' 
+			and date_sub(curdate(),interval 15 Day) > modified""")
+
+def get_support_mails():
+	if cint(webnotes.conn.get_value('Email Settings', None, 'sync_support_mails')):
+		SupportMailbox()
\ No newline at end of file
diff --git a/support/doctype/support_ticket/support_ticket.js b/support/doctype/support_ticket/support_ticket.js
index 159dddd..28b08f8 100644
--- a/support/doctype/support_ticket/support_ticket.js
+++ b/support/doctype/support_ticket/support_ticket.js
@@ -49,11 +49,17 @@
 		var wrapper = cur_frm.fields_dict['thread_html'].wrapper;
 		
 		var comm_list = wn.model.get("Communication", {"support_ticket": doc.name})
-		comm_list.push({
-			"sender": doc.raised_by,
-			"creation": doc.creation,
-			"modified": doc.creation,
-			"content": doc.description});
+
+		var sortfn = function (a, b) { return (b.creation > a.creation) ? 1 : -1; }
+		comm_list = comm_list.sort(sortfn);
+		
+		if(!comm_list.length || (comm_list[0].sender != doc.raised_by)) {
+			comm_list.push({
+				"sender": doc.raised_by,
+				"creation": doc.creation,
+				"modified": doc.creation,
+				"content": doc.description});
+		}
 					
 		cur_frm.communication_view = new wn.views.CommunicationList({
 			list: comm_list,