refactoring - combining 3 functions into 1