Add more type constraint and refactoring