Shouldn't `mask` be an instance variable that you update in conjunction with `capacity`?