Move functions to autoload

Clean up functions, move everything into autoload/, move code around a
little bit.