tidyed up code