refactoring code to better architecture