use writelines instead of hand written for loop writes