Refactor code

Add more type check functions.
Add more comments