Make read function more robust