Updated functions to get them to work properly, several changes