Refactor percent function