rewrote main function