updated version, cleaner code