Fix minor typos and refactor logic