fixed bugs with lastindexof and added more error checking