Fix of memory issues and connection handling