refactor inline php into functions in misc.php