improve width algorithm