reordering code to define functions before they're called (causes problems in firefox otherwise)