re-wrote in functional apporach