tidyed up logic