refactored random code