Added writeCurrentState

Added function to record current state to a text file
modularize writeCurrentState
refactored