Refactor code for improved efficiency