moved around a little code to improve readability