Moved code around such that comments make sense