some refactoring to streamline the code