main: factored out main loop from top-level

Signed-off-by: Sergei Trofimovich <slyfox@gentoo.org>