Added error checking and fixed all issues