Move function to library and rename it

Signed-off-by: Marc Delisle <marc@infomarc.info>
Fix coding style

Signed-off-by: Marc Delisle <marc@infomarc.info>