Trying to get the functions to work, and minor refactoring