better implementation of segment writer