Converted subroutines into functions