Further edits. Need to take into account the TO DO notes, for now there are ugly workarounds.