Fixed the casting issue and added some comments.