moved code around a bit