cleaned up function to prepare for refactoring