edit much more clear and less buggy