added writetofile and added run() functionality