Bugfix for writing collections. Should add tests and then make a pull
request.