added writer, fixed some bugs