Move functions into separate files, improve upper-variable reference detection (still needs improvement)