Added some checks to handle invalid input