Fix negative number of lines handling & reorganise functions order