Changed some code from printf to write