better way to solve this problem