separated writing to file
fixed some bugs lol should have tested first