add more inline comments and use isinstance