refactoring code for a simple solution