Implemented file writing logic.