Added some comments and also some error handling logic.