fixed potential indexing into an empty list