added file writing functionality, and fixed minor things