refactoring with better code