greatly simplified the code by using the file-kit library and thinking a bit more of what I was doing