beggin refactoring code