refactor code for shorter function