added much cleaner code to do the same stuff