Fixed issues, added some checks