Fix code style and extract to a separate method to improve readability