refator into functions