added saving feature, also adjusted code to be more readbale