refactoring the function to be a little less ugly