Almost done. Needs more testing when inserting stuff into the viwer.