My attempt at writing the functions (and it's really close)