Modified function as in prev commit, fixed small mistake.