trying original solution from stackoverflow