Abstracted main_loop and fixed indentation