refactored to streamline code