better logic implemented