Simplified hostname sed and fixed potential bugs