fixed bug with multiple results in single popup, refactored code