added few more checks to make it fool proof