some refactoring to make the overall structure more clear