added file writing code, fixed bugs, removed print statements