cleaning up code to use a function instead of repeating