refactored filewriter to first write to buffer before flushing to SD card