refactored main loo