Re-organized code and added unread message counter