some refactoring to the type cast pull request