moved function defs to the top of the file