Changed code organization for functions