reorganized the functions slightly