add writer factory, reformat code