refactoring toInt method