Cleanup some code, fix socket shutdown