slightly better square algorithm