version with functions in the right place