Updated code, adapted for multiple elements usage