moved function declaration to top to try and stop a stupid bug only happening in production