Cleaned up code so that it makes more sense.