Refactoring gui.

Moving some toplevel functions to their own file.