re-factored to make everything in one place