Rewrote everything into main function