better solution to previous problem