initial code refactored