Master version of some fixes by myshkin on restruct