Refactored stuff out of main