added saving functionality
corrected main function