rearranged code
add subroutines