refactored after working code