clean up, splitted code into functions