Moved around logic so it runs correctly