Working code but not efficient yet