Several code fixes