adding writer function
fixing some bugs