tryed to be more functional (and fix some bugs)