Added check for empty array, only single return statement, moved variables down into else.