Moved code around, some cleanup