replaced some repeated code by function calls