Code restyle, added some checks