trying some ideas from stackoverflow