Improve ratio calculation

Fix #1022