Code cleaning, moving some functions to utils. (see issue #1)