Implemented write_data function