Refactored first_nonrepeated_character function