refactoring and structuring some code