cleaned up main function