move helper functions to functions.inc, write sessions.php, TODO: document and test