avoid indexing invalid coordinates. other minor changes