Added some checks and fixed some bugs