general bin function