Some improvments to code, especially when replacing NAs values