actually put the functions in, fixed some bugs