cleaned up the code a bit

added a bit of file error handeling