Moved some common functionality into helper function