changes to mimic the stackoverflow solution