Added ApplicationMain and moved some non-UI to it