Refactor by adding a utility function to return the contents of a buffer as a string
Remove duplicate function definition