Basic writing version almost working. A couple of bugs (addressed with FIXME in the code) still remain