Added some major coding changes,

fixed crashing upon connection error bug